// 格式化日期为 YYYY-MM-DD
function formatDate(date: Date): string {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}

// 格式化时间为 HH:MM:SS
function formatTime(date: Date): string {
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");
  return `${hours}:${minutes}:${seconds}`;
}

// 格式化时间戳为 YYYY-MM-DD HH:MM:SS
function formatDateTime(timestamp: number): string {
  const date = new Date(timestamp);
  return `${formatDate(date)} ${formatTime(date)}`;
}

// 将时间戳格式化为 YYYY-MM-DD HH:MM:SS
function formatTimestamp(timestamp: number): string {
  const date = new Date(timestamp);
  return `${formatDate(date)} ${formatTime(date)}`; // 格式化日期和时间
}

// 计算两个日期之间的天数差
function daysBetween(date1: Date, date2: Date): number {
  const diffTime = Math.abs(date2.getTime() - date1.getTime());
  return Math.floor(diffTime / (1000 * 60 * 60 * 24));
}

// 获取今天是星期几
function getWeekday(date: Date): string {
  const weekday = [
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
  ];
  return weekday[date.getDay()];
}

// 获取指定月份的天数
function getMonthDays(year: number, month: number): number {
  const days = new Date(year, month + 1, 0).getDate();
  return days;
}

// 添加天数到指定日期
function addDays(date: Date, days: number): Date {
  const result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

// 检查日期是否在范围内
function isDateInRange(date: Date, start: Date, end: Date): boolean {
  return date >= start && date <= end;
}

//   将格式化日期转为 Date 对象 YYYY-MM-DD HH:MM:SS
function parseDate(dateStr: string): Date {
  const year = parseInt(dateStr.slice(0, 4));
  const month = parseInt(dateStr.slice(5, 7)) - 1;
  const day = parseInt(dateStr.slice(8, 10));
  const hours = parseInt(dateStr.slice(11, 13));
  const minutes = parseInt(dateStr.slice(14, 16));
  const seconds = parseInt(dateStr.slice(17, 19));
  return new Date(year, month, day, hours, minutes, seconds);
}

export {
  formatDate,
  formatTime,
  formatDateTime,
  formatTimestamp,
  daysBetween,
  getWeekday,
  getMonthDays,
  addDays,
  isDateInRange,
  parseDate,
};
